package com.xy.manager;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.a520it.generalutils.R;
import com.xy.debug.LogUtils;

/**
 * Created by John on 2016/10/27.
 */

public abstract class FragmentTabManager {

    private FragmentManager mFragmentmanger;  // Fragment所属的Activity
    private int fragmentContentId;  // Activity中所要被调换的的布局id
    private int currentIndex = -1;   //当前页数索引
    private ExtraCheckChangedListener extraCheckChangedListener;  //item切换时功能拓展接口
    private Fragment mCurrentPrimaryItem = null;


    public FragmentTabManager(FragmentManager mFragmentmanger, int fragmentContentId) {
        this.mFragmentmanger = mFragmentmanger;
        this.fragmentContentId = fragmentContentId;
    }

    protected abstract Fragment getNewFragment(int index);

    public void check(int index, boolean anim) {
        if (setPageByIndex(index, anim) && extraCheckChangedListener != null) {
            extraCheckChangedListener.checkChanged();
        }
    }

    /**
     * 设置当前显示的页面
     **/
    public boolean setPageByIndex(int index, boolean anim) {
        if (currentIndex == index) {
            return false;
        }
        FragmentTransaction transaction = obtainFragmentTransaction(index < currentIndex, anim);
        if (mCurrentPrimaryItem != null) {
            transaction.hide(mCurrentPrimaryItem);
        }

        String name = makeFragmentName(fragmentContentId, index);
        Fragment fragment = mFragmentmanger.findFragmentByTag(name);
        if (fragment != null) {
            transaction.show(fragment);
            View view = fragment.getView();
            if (view != null) {
                view.bringToFront();
            }
        } else {
            fragment = getNewFragment(index);
            transaction.add(fragmentContentId, fragment, name);
        }
        currentIndex = index;
        mCurrentPrimaryItem = fragment;
        transaction.commitAllowingStateLoss();
        return true;
    }

    /**
     * fragment的切换
     */
    public void replaceFragment(Fragment fragment, int index) {
        replaceFragment(null, fragment, index);
    }

    public void replaceFragment(FragmentTransaction ts, Fragment fragment, int index) {
        String name = makeFragmentName(fragmentContentId, index);
        Fragment oldFragment = mFragmentmanger.findFragmentByTag(name);
        if (oldFragment != null) {
            if (oldFragment == mCurrentPrimaryItem) {
                if (ts == null) {
                    ts = mFragmentmanger.beginTransaction();
                    ts.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                }
                ts.remove(oldFragment);
                ts.add(fragmentContentId, fragment, name);
                mCurrentPrimaryItem = fragment;
                ts.commitAllowingStateLoss();
            } else {
                ts = mFragmentmanger.beginTransaction();
                ts.remove(oldFragment);
                ts.commitAllowingStateLoss();
            }
        }
    }

    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }

    /**
     * 设置fragment切换动画
     **/
    private FragmentTransaction obtainFragmentTransaction(boolean left, boolean hasAnim) {
        FragmentTransaction ft = mFragmentmanger.beginTransaction();
        if (hasAnim) {
            if (left) {
                ft.setCustomAnimations(R.anim.slide_in_left_fragment, R.anim.slide_out_right_fragment);
            } else {
                ft.setCustomAnimations(R.anim.slide_in_right_fragment, R.anim.slide_out_left_fragment);
            }
        } else {
            ft.setCustomAnimations(0, 0);
        }
        return ft;
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public ExtraCheckChangedListener getExtraCheckChangedListener() {
        return extraCheckChangedListener;
    }

    public void setExtraCheckChangedListener(ExtraCheckChangedListener extraCheckChangedListener) {
        this.extraCheckChangedListener = extraCheckChangedListener;
    }

    interface ExtraCheckChangedListener {
        void checkChanged();
    }
}
